
package br.unipampa.view;

import br.unipampa.model.Data;
import br.unipampa.utilitarios.ExecutaSom;
import br.unipampa.utilitarios.ImplementaIcone;
import br.unipampa.utilitarios.TrataAlarme;
import br.unipampa.utilitarios.Utilitaria;
import br.unipampa.utilitarios.VerificarCampos;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * 
 * JFrame responsável por cadastrar compromissos, através dele o usuário
 * insere as informações básicas de um novo compromisso a ser agendado.
 * 
 * @author Gabriel Moro
 * @since 15/03/2013
 * @version 1.2
 */
public class CadastroDeCompromisso extends javax.swing.JFrame implements VerificarCampos,ImplementaIcone,EfeitosSonoros{
//ATRIBUTOS
    private Data date;
    private String dia;
    private String mes;
    private String ano;
    private Image iconeTitulo;

    /**
     * Método construtor responsável por inicializar os devidos componentes
     */
    public CadastroDeCompromisso(String dia,String mes,String ano) {

        initComponents();
        this.dia= dia;
        this.mes= mes;
        this.ano= ano;
        
        revalidaFrame();
        
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                ApoioDeInterface.mensagemSair();
            }
        });

        tratarIcone();
        setResizable(false);
        setLocationRelativeTo(null);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnVoltar = new javax.swing.JButton();
        jPanelInterno = new javax.swing.JPanel();
        lblCompromisso = new javax.swing.JLabel();
        txtCompromisso = new javax.swing.JTextField();
        lblDia = new javax.swing.JLabel();
        lblHora = new javax.swing.JLabel();
        lblAno = new javax.swing.JLabel();
        btnSalvar = new javax.swing.JButton();
        jComboHora = new javax.swing.JComboBox();
        lblMes = new javax.swing.JLabel();
        lblDiaEditar = new javax.swing.JLabel();
        lblMesEditar = new javax.swing.JLabel();
        lblAnoEditar = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Cadastro de Compromissos");

        btnVoltar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/unipampa/icons/voltar.png"))); // NOI18N
        btnVoltar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnVoltarActionPerformed(evt);
            }
        });

        jPanelInterno.setBackground(new java.awt.Color(204, 204, 255));

        lblCompromisso.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        lblCompromisso.setText("Compromisso:");

        lblDia.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        lblDia.setText("Dia:");

        lblHora.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        lblHora.setText("Hora:");

        lblAno.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        lblAno.setText("Ano:");

        btnSalvar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/unipampa/icons/saveCadastro.png"))); // NOI18N
        btnSalvar.setToolTipText("Salvar");
        btnSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalvarActionPerformed(evt);
            }
        });

        jComboHora.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "-------", "00:00", "00:30", "01:00", "01:30", "02:00", "02:30", "03:00", "03:30", "04:00", "04:30", "05:00", "05:30", "06:00", "06:30", "07:00", "07:30", "08:00", "08:30", "09:00", "09:30", "10:00", "10:30", "11:00", "11:30", "12:00", "12:30", "13:00", "13:30", "14:00", "14:30", "15:00", "15:30", "16:00", "16:30", "17:00", "17:30", "18:00", "18:30", "19:00", "19:30", "20:00", "20:30", "21:00", "21:30", "22:00", "22:30", "23:00", "23:30", "23:45" }));

        lblMes.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        lblMes.setText("Mês:");

        lblDiaEditar.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        lblDiaEditar.setText("<Dia>");

        lblMesEditar.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        lblMesEditar.setText("<Mes>");

        lblAnoEditar.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        lblAnoEditar.setText("<Ano>");

        javax.swing.GroupLayout jPanelInternoLayout = new javax.swing.GroupLayout(jPanelInterno);
        jPanelInterno.setLayout(jPanelInternoLayout);
        jPanelInternoLayout.setHorizontalGroup(
            jPanelInternoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelInternoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelInternoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelInternoLayout.createSequentialGroup()
                        .addComponent(lblCompromisso)
                        .addGap(18, 18, 18)
                        .addComponent(txtCompromisso))
                    .addGroup(jPanelInternoLayout.createSequentialGroup()
                        .addGroup(jPanelInternoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanelInternoLayout.createSequentialGroup()
                                .addComponent(lblAno)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(lblAnoEditar))
                            .addGroup(jPanelInternoLayout.createSequentialGroup()
                                .addComponent(lblMes)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(lblMesEditar))
                            .addGroup(jPanelInternoLayout.createSequentialGroup()
                                .addGroup(jPanelInternoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(lblHora)
                                    .addComponent(lblDia))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(jPanelInternoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jComboHora, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(lblDiaEditar)))
                            .addComponent(btnSalvar, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 335, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanelInternoLayout.setVerticalGroup(
            jPanelInternoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelInternoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelInternoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblCompromisso)
                    .addComponent(txtCompromisso, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(29, 29, 29)
                .addGroup(jPanelInternoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblHora)
                    .addComponent(jComboHora, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanelInternoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblDia)
                    .addComponent(lblDiaEditar))
                .addGap(16, 16, 16)
                .addGroup(jPanelInternoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblMes)
                    .addComponent(lblMesEditar))
                .addGap(13, 13, 13)
                .addGroup(jPanelInternoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblAno)
                    .addComponent(lblAnoEditar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 21, Short.MAX_VALUE)
                .addComponent(btnSalvar, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(btnVoltar, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(19, 19, 19)
                        .addComponent(jPanelInterno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(24, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnVoltar, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanelInterno, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnVoltarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnVoltarActionPerformed
        efeitoSonoro();
        CadastroDeCompromisso.this.dispose();
    }//GEN-LAST:event_btnVoltarActionPerformed

    private void btnSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalvarActionPerformed
        efeitoSonoro();
        if (verificaCampos()) {
            String tarefas = txtCompromisso.getText();
            //TRATANDO HORA
            Object hora = jComboHora.getSelectedItem();
            String horaTxt = String.valueOf(hora);
            
            int diaInteiro=Utilitaria.converteParaInteiro(this.dia);
            this.date = new Data(diaInteiro, this.mes, this.ano, horaTxt, tarefas);
            if (this.date.salvarDados()) {
                ApoioDeInterface.mensagemDeSucesso();
                TrataAlarme.getInstance().addHorarioCompromisso(horaTxt, tarefas);
                CadastroDeCompromisso.this.dispose();
            }
        }
    }//GEN-LAST:event_btnSalvarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnSalvar;
    private javax.swing.JButton btnVoltar;
    private javax.swing.JComboBox jComboHora;
    private javax.swing.JPanel jPanelInterno;
    private javax.swing.JLabel lblAno;
    private javax.swing.JLabel lblAnoEditar;
    private javax.swing.JLabel lblCompromisso;
    private javax.swing.JLabel lblDia;
    private javax.swing.JLabel lblDiaEditar;
    private javax.swing.JLabel lblHora;
    private javax.swing.JLabel lblMes;
    private javax.swing.JLabel lblMesEditar;
    private javax.swing.JTextField txtCompromisso;
    // End of variables declaration//GEN-END:variables


    @Override
    public boolean verificaCampos() {
        if (txtCompromisso.getText() != null
                || !txtCompromisso.getText().isEmpty()
                || jComboHora.getSelectedIndex() != 0) {
            return true;
        } else {
            return false;
        }
    }
    private void revalidaFrame(){
        if(this.dia!=null||this.mes!=null||this.ano!=null){
            this.lblDiaEditar.setText(this.dia);
            this.lblMesEditar.setText(this.mes);
            this.lblAnoEditar.setText(this.ano);
        }
    }
     @Override
    public void tratarIcone() {
        //definindo icone do sistema
        this.iconeTitulo= Toolkit.getDefaultToolkit().getImage(
                getClass().getResource("/br/unipampa/icons/iconeOfSystem.png"));
        
        this.setIconImage(this.iconeTitulo);
    }

    @Override
    public void efeitoSonoro() {
        ExecutaSom exeSound= new ExecutaSom();
        exeSound.executar("/br/unipampa/sounds/click1.wav");
    }
}